#include "votedialog.h"
#include "director.h"
#include "ui_votedialog.h"
#include <QButtonGroup>
#include <QMessageBox>
#include <QRadioButton>
VoteDialog::VoteDialog(QWidget *parent)
    : QDialog(parent), ui(new Ui::VoteDialog) {
  ui->setupUi(this);

  Qt::WindowFlags flages = windowFlags();
  setWindowFlags(
      flages & ~(Qt::WindowCloseButtonHint | Qt::WindowContextHelpButtonHint));

  QBoxLayout *layout = new QVBoxLayout(ui->userList);
  btnGrp = new QButtonGroup(ui->userList);
  int i = 0;
  QRadioButton *firstBtn = nullptr;
  for (User *user : Director::instance()->users) {
    if (user->out)
      continue;
    QRadioButton *btn = new QRadioButton(user->nick, this);
    if (firstBtn == nullptr)
      firstBtn = btn;
    btn->setIcon(QIcon(""));
    btnGrp->addButton(btn, i++);
    layout->addWidget(btn);
  }

  // default
  btnGrp->button(0)->click();
}

VoteDialog::~VoteDialog() { delete ui; }

void VoteDialog::on_pushButton_clicked() {
  QAbstractButton *selButton = btnGrp->checkedButton();
  if (selButton != nullptr) {
    Director::instance()->sendTicket(selButton->text());
    this->accept();
    return;
  }
  QMessageBox::information(this, "提示", "请选择你要投出的用户！");
}
